/**********
    @name: depart科室选择组件
    @describtion:
    @author:liubingye
    @date:2023-03-03 11:18:10
***********/
<template>
  <el-form-item class="ruifox-depart-mini" :label="props.title" :rules="departRule" :prop="props.field">
    <el-popover
        :visible="popoverVisible"
        trigger="focus"
        :width="700"
    >
      <div class="inner-depart-option">
        <div class="clear-all">
          <el-button type="primary" link @click="clearChoosed">清空</el-button>
          <el-button type="primary" link @click="closePopover">确定</el-button>
        </div>
        <el-tabs v-model="activeName" @tab-change="handleTabChange">
          <el-tab-pane v-for="item in props.options" :label="item.area" :key="item.id" :name="item.id">
            <el-scrollbar height="300px">
              <div class="depart-wrap" v-for="departClass in item.depart_list" :key="departClass.class">
                <h4 class="depart-class-title">{{departClass.class}}</h4>
                <div class="depart-class-list">
                  <span class="depart" :class="{'is-chose': judgeChose(depart)}" v-for="depart in departClass.depart" :key="depart.id" @click="handleChooseDepart(depart)">{{depart.name}}</span>
                </div>
              </div>
            </el-scrollbar>
          </el-tab-pane>
        </el-tabs>
      </div>
      <template #reference>
        <div ref="popReference" :class="{'is-focus': selectFocus, 'multiple-disabled': !hasChoosed}" class="depart-select" tabindex="-1" @focus="handleFocusDepart" @blur="handleBlurDepart">
          <div class="depart-container">
            <span class="chose-depart" v-for="item in choseDepartList.slice(0, 1)" :key="item.id">
              {{item.name}}
            </span>
            <span v-if="choseDepartList.length > 1" class="chose-depart chose-depart-num">
              <el-icon :size="12"><Plus /></el-icon>{{choseDepartList.length - 1}}
            </span>
          </div>
          <div class="choose-depart" v-if="choseDepartList.length < 1">
            <span>请选择{{props.title}}</span>
            <i class="iconfont">&#xe678;</i>
          </div>
        </div>
      </template>
    </el-popover>
  </el-form-item>
</template>

<script setup>
import {ref, onMounted, computed, watch} from "vue";
import { Plus } from '@element-plus/icons-vue'
const props = defineProps({
  title: {
    type: String,
    default: ''
  },
  value: {
    type: Array,
    default: null
  },
  options: {
    type: Array,
    default: null
  },
  field: {
    type: String,
    default: ''
  },
  required: {
    type: Boolean,
    default: false
  },
  hasChoosed: {
    type: Boolean,
    default: true
  },
})
const hasChoosed = computed(()=>{return props.hasChoosed})
const popReference = ref()
const popoverVisible = ref(false)
// const choseDepartList = ref(props.value ? props.value : [])
const choseDepartList = computed({
  get: () => props.value ? props.value : [],
  set: val =>  emits('update:value', val)
})
const emits = defineEmits(['update:value','getValue'])

const activeName = computed(()=>{return props.options?.length ? props.options[0].id : null})
const selectFocus = ref(false)

const handleFocusDepart = () => {
  selectFocus.value = true
  popoverVisible.value = true
}
const handleTabChange = () => {
  popReference.value.focus()
}
const handleBlurDepart = () => {
  selectFocus.value = false
  popoverVisible.value = false
}
const handleChooseDepart = (depart) => {
  popReference.value.focus()
  if(choseDepartList.value.some((item) => {
    return item.id === depart.id
  })){
    choseDepartList.value = choseDepartList.value.filter(item => {
      return item.id !== depart.id
    })
  }else{
    choseDepartList.value.push(depart)
  }
  // emits('update:value',choseDepartList.value)
}
const judgeChose = (depart) => {
  return choseDepartList.value.some((item) => {return item.id === depart.id})
}
// const handleCancel = (depart) => {
//   popoverVisible.value = false
//   popReference.value.blur()
//   choseDepartList.value = choseDepartList.value.filter(item => {return item.id !== depart.id})
// }
const departRule = ref([])
const validateDepart = (rule, value, callback) => {
  if (!value || value.length === 0) {
    callback(new Error(props.title + '不能为空'))
  } else {
    callback()
  }
}
const clearChoosed = () => {
  emits('update:value', [])
}
const closePopover = () => {
  popoverVisible.value = false
}
watch(() => choseDepartList.value,newVal => {
  emits('getValue',{field: props.field,value: newVal.map(item => item.id)})
},{deep: true})
onMounted(() => {
  if(props.required){
    departRule.value.push({validator: validateDepart,trigger: 'blur'})
  }
})
</script>

<style scoped lang="scss">
.inner-depart-option{
  position: relative;
  .clear-all{
    position: absolute;
    top: 0px;
    right: 0;
    cursor: pointer;
    z-index: 9999;
  }
}
.depart-select{
  width: 100%;
  height: 32px;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  box-sizing: border-box;
  padding: 0 10px;
  position: relative;
  .choose-depart{
    position: absolute;
    top: 0;
    right: 10px;
    display: flex;
    flex-shrink: 0;
    span{
      color: $mainColor;
    }
    i{
      margin-left: 10px;
      font-size: 16px;
      color: #CACED4;
    }
  }
  &:hover{
    border-color: #c0c4cc;
  }
  &.is-focus{
    border-color: $mainColor !important;
  }
  .depart-container{
    display: flex;
    min-width: 80px;
    .chose-depart{
      display: inline-block;
      padding: 0 5px;
      height: 20px;
      line-height: 20px;
      margin: 0px 3px;
      border-radius: 5px;
      background-color: #f0f2f5;
      flex-shrink: 0;
      cursor: default;
      width: 100px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      i{
        cursor: pointer;
        margin-left: 5px;
      }
    }
    .chose-depart-num{
      width: 44px;
      padding: 0 3px;
    }
  }
}
.depart-wrap{
  display: flex;
  .depart-class-title{
    color: $mainColor;
    width: 20px;
    flex-shrink: 0;
  }
  .depart-class-list{
    display: flex;
    flex-wrap: wrap;
    span.depart{
      display: block;
      padding: 0 5px;
      height: 20px;
      line-height: 21px;
      margin: 0 10px 10px 0;
      border-radius: 5px;
      cursor: pointer;
      &:hover{
        background-color: $borderColor;
      }
    }
    span.depart.is-chose{
      background-color: $mainColor!important;
      color: $white;
    }
  }
}
.el-form-item{
  margin-bottom: 0 !important;
}
.multiple-disabled{
  background: #F5F7FA !important;
  color: #999 !important;
  pointer-events: none;
  cursor: not-allowed !important;
  span{
    color: #999 !important;
  }
}
</style>
